//
//  CNMTopLabel.swift
//  CNMTools
//
//  Created by MrHong on 2023/11/10.
//

import UIKit

public class CNMTopLabel: UILabel {
    public var topMargin: CGFloat // 距离顶部的距离，外界可以随意设置
    
    // 通过约束布局用这个
    public init(topMargin: CGFloat = 0) {
        self.topMargin = topMargin
        super.init(frame: .zero)
    }
    // 通过frame 布局用这个
    public init(frame: CGRect,topMargin: CGFloat = 0) {
        self.topMargin = topMargin
        super.init(frame: frame)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
        var textRect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
        textRect.origin.y = bounds.origin.y + topMargin
        return textRect
    }
    
    public override func drawText(in rect: CGRect) {
        let actualRect = textRect(forBounds: rect, limitedToNumberOfLines: self.numberOfLines)
        super.drawText(in: actualRect)
    }
}
